using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace homework7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.BackgroundImage = Image.FromFile("D:\\Desktop\\1-2012221T114.jpg");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task searchbaidu = new Task(() =>
              {
                  WebClient wc = new WebClient();
                  string url = "https://baike.baidu.com/item/" + Input.Text;
                  byte[] receiveData1 = wc.DownloadData(url);
                  string result = Encoding.UTF8.GetString(receiveData1);
                  baidutext.Text = Filter(result, 200);
              });

            Task searchbing = new Task(() =>
            {
                WebClient wc = new WebClient();
                string url = "https://cn.bing.com/search?form=MOZTSB&pc=MOZI&q=" + Input.Text;
                byte[] receiveData2 = wc.DownloadData(url);
                string result = Encoding.UTF8.GetString(receiveData2);
                bingtext.Text = Filter(result, 200);
            });
                searchbaidu.Start();
                searchbing.Start();
        }

        public static string Filter(string oriText,int length)
        {
            MatchCollection Matches = Regex.Matches
            (oriText, @"[\u4E00-\u9FFF]+", RegexOptions.IgnoreCase);
            StringBuilder sb = new StringBuilder();
            foreach (Match m in Matches)
            {
                sb.Append(m.Value);
            }
            string s = sb.ToString();
            if (length > 0 && s.Length > length)
                return s.Substring(0, length);
            return s;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}